var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_page:1
  },



  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {

    if (options) {
      wx.setStorageSync("tmp_options", options)
    }
    this.getCollect();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.page_info.total_page> this.data.current_page){
      this.setData({
        current_page: this.data.current_page+1
      })
      this.getCollect()
    }else{
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },


  //获取收藏列表
  getCollect: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })

    app.request({
      url: api.paper.mistake_list,
      method: "POST",
      data:{
        page:that.data.current_page
      },
      success: function (response) {
        if (response.errcode == 0) {
          if(that.data.current_page>1){
            var list = that.data.list;
            var new_list = list.concat (response.data.list);
            that.setData({
              list: new_list,
              page_info: response.data.page_info,
            });
          }else{
            that.setData({
              list: response.data.list,
              page_info: response.data.page_info,
            });
          }
         
        } else {
          wx.showToast({
            title: response.errmsg,
            icon: 'none'
          })
        }

      },
      complete: function () {
        wx.hideLoading();
      }
    });

  },
  /** 
   * 删除错题
   */
  delMistake(e){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })

    app.request({
      url: api.paper.del_mistake,
      method: "POST",
      data: {
        id: e.currentTarget.dataset.id
      },
      success: function (response) {
        if (response.errcode == 0) {
          var list = that.data.list;
          var idx = e.currentTarget.dataset.key
          list.splice(idx, 1);
          console.log(idx)
          that.setData({
            list:list
          }) 

        } else {
          wx.showToast({
            title: response.errmsg,
            icon: 'none'
          })
        }

      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },



})